<?php
class member extends Front_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('users/users_model','user');
		$this->load->model('knowledge/knowledge_model','knowledge');
		$this->load->model('users/user_knowledgetype_model','u_knowledge');
		$this->load->model('request_con_model','request_con');
		$this->load->model('request_model','request');
		$this->load->model('knowledge/knowledgefiles_model','knowledgefiles');
	}
	
	function profile(){
		$data='';
		$this->template->build('profile',$data);
	}
	
	function request(){
		$data['requests'] = $this->request->where("req_user_id = ".login_data('id'))->get();
		$this->template->build("request",$data);
	}
	
	function request_detail($id=FALSE){
		$data['detail'] = $this->request->get_row($id);
		$data['request_cons'] = $this->request_con->where('request_id = '.$id)->order_by('id','asc')->limit(9999)->get();
		$this->template->build('request_detail',$data);		
	}
	
	function approve_request($id=FALSE){
		$data['requests'] = $this->request->where("own_user_id = ".login_data('id'))->get();
		$this->template->build('approve',$data);		
	}
	
	function approve_detail($id=FALSE){
		$data['detail'] = $this->request->get_row($id);
		$data['request_cons'] = $this->request_con->where('request_id = '.$id)->order_by('id','asc')->limit(9999)->get();
		$this->template->build('approve_detail',$data);
	}

	function request_con_save(){
		if($_POST){
			
			// บันทึกคำขอ
			if($_POST['detail']  != ""){
				$_POST['created'] = date("Y-m-d H:i:s");
				$_POST['sender'] = login_data('id');
				$this->request_con->save($_POST);
				
				$user = $this->user->get_row($_POST['recipients']);
				
				$type = ($_POST['type'] == 'request')?"approve_detail":"request_detail";
				$link = $_POST['mail_url'].'/phklb/member/'.$type.'/'.$_POST['request_id'];
				
				$this->send_mail_phpmailer($user['email'],$_POST['mail_kntitle'],$_POST['mail_author'],$_POST['detail'],$link);
			}
			
			// ยันทึกสถานะ
			$_POST['id'] = $_POST['request_id'];
			$this->request->save($_POST);
			
			set_notify('success', 'บันทึกข้อมูลเรียบร้อยแล้ว');
		}
		redirect($_POST['referer']);
	}

	function send_mail_phpmailer($email,$kn_title,$author,$detail,$link){
		require("include/PHPMailer/class.phpmailer.php");
		$mail = new PHPMailer();
		
		$body = 'ขออนุญาตใช้ข้อมูล<br><br>';
		$body .= 'เรื่อง : '.$kn_title.'<br><br>';
		$body .= 'ผู้แต่ง/ผู้จัดทำ : '.$author.'<br><br>';
		$body .= 'ข้อความ : '.$detail.'<br><br>';
		$body .= '<a href="'.$link.'">'.$link.'</a>';
		
		$mail->CharSet = "utf-8";
		$mail->IsSMTP();
		$mail->SMTPDebug = 0;
		$mail->SMTPAuth = true;
		$mail->Host       = "smtp.mail.yahoo.com";
		$mail->Port       = 25;
		$mail->Username   = "favouritedesign18@yahoo.com";
		$mail->Password   = "55555555";
		
		$mail->SetFrom('favouritedesign18@yahoo.com', 'webmaster@ph.mahidol.ac.th');
		$mail->AddReplyTo("favouritedesign18@yahoo.com", "favouritedesign18@yahoo.com");
		$mail->Subject = "ขออนุญาตใช้งานข้อมูล - ระบบคลังความรู้เพื่อสนับสนุนงานสาธารณสุข คณะสาธารณสุขศาสตร์ มหาวิทยาลัยมหิดล";
		
		$mail->MsgHTML($body);
		
		$mail->AddAddress($email, $email); // ผู้รับคนที่หนึ่ง
		
		if(!$mail->Send()) {
		    echo "Mailer Error: " . $mail->ErrorInfo;
		} else {
		    echo "Message sent!";
		}
	}
}
?>